package cn.ctyun.player.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.ctyun.player.R;
import cn.ctyun.videoplayer.bean.DanmakuDetail;
import cn.ctyun.videoplayer.bean.DanmakuModel;
import cn.ctyun.videoplayer.bean.VideoModel;
import cn.ctyun.videoplayer.listener.AdControllerListener;
import cn.ctyun.videoplayer.listener.MarqueeViewListener;
import cn.ctyun.videoplayer.listener.OnEnterFullScreenListener;
import cn.ctyun.videoplayer.listener.OnErrorListener;
import cn.ctyun.videoplayer.listener.OnExitFullScreenListener;
import cn.ctyun.videoplayer.listener.OnLoadStartListener;
import cn.ctyun.videoplayer.listener.OnMuteListener;
import cn.ctyun.videoplayer.listener.OnPauseAdClickListener;
import cn.ctyun.videoplayer.listener.OnPauseListener;
import cn.ctyun.videoplayer.listener.OnPlayListener;
import cn.ctyun.videoplayer.listener.OnProgressChangeListener;
import cn.ctyun.videoplayer.listener.OnResumeVolumeListener;
import cn.ctyun.videoplayer.listener.OnSeekToListener;
import cn.ctyun.videoplayer.listener.OnSetVolumeListener;
import cn.ctyun.videoplayer.listener.OnStopListener;
import cn.ctyun.videoplayer.player.PlayerConfig;
import cn.ctyun.videoplayer.util.PlayerConstants;
import cn.ctyun.videoplayer.util.MemoryLeakUtil;
import cn.ctyun.videoplayer.widget.VideoView;


public class DemoActivity extends AppCompatActivity {
    private static final String TAG = DemoActivity.class.getSimpleName();

    private VideoView mVideoView;
    private static final String URL_AD = "http://help.qhoss.xstore.ctyun.cn/video/ad.mp4";
    private static final String pauseAdUrl = "http://s305.photo.store.qq.com/psb?/V10eAGEV1otPhC/kBAaz7ARkoq*denFUikaEZO12W2Ij7FLSK5w0P08vA4!/b/dDEBAAAAAAAA&bo=0gHaANIB2gACkLQ!&_abc=.gif";
    private static final String pauseAdUrl2 = "https://www.ctyun.cn/gw/cms/material/icon/GetByFileId?fileId=153423425195474944";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        Intent intent = getIntent();
        String vodUrl = intent.getStringExtra("url");

        mVideoView = findViewById(R.id.player);

        // 设置播放器的高度，一般宽高比为16:9
        int widthPixels = getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();
        layoutParams.height = widthPixels / 16 * 9;

        // 可选：播放器相关配置，其他配置参照类PlayerConfig
        mVideoView.setPlayerConfig(new PlayerConfig.Builder()
//                .autoRotate(false) // 自动旋转屏幕
//                .enableMediaCodec() // 启动硬解码
                .savingProgress() // 保存播放进度
                .enableCache()
                .build());

        // 设置播放源
        List<VideoModel> videos = new ArrayList<>();
        // 片头广告
        videos.add(new VideoModel(URL_AD, "", PlayerConstants.VideoType.START_AD, new AdControllerListener() {
            @Override
            public void onAdClick() {
                Toast.makeText(DemoActivity.this, "广告点击跳转", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelAdClick() {
                mVideoView.skipToNext();
            }
        }));
        videos.add(new VideoModel(vodUrl, "这是一个标题", PlayerConstants.VideoType.VOD));
        // 片尾广告
        videos.add(new VideoModel(URL_AD, "", PlayerConstants.VideoType.END_AD));

        mVideoView.fromSource(videos)
                .pauseAdUrl(pauseAdUrl)
                .pauseAd(pauseAdUrl, new OnPauseAdClickListener() {
                    @Override
                    public void click() {
                        Toast.makeText(DemoActivity.this, "广告点击跳转", Toast.LENGTH_SHORT).show();
                    }
                })
                .thumbUrl(pauseAdUrl2)
                .watermark("我的水印", PlayerConstants.WatermarkTextPosition.BOTTOM_RIGHT, "#ffffffff", 15)
//                .playableVideoDuration(10) // 设置视频最长播放时长

                .onPlay(new OnPlayListener() {
                    @Override
                    public void play() {
                        Log.d(TAG, "onPlay");
                    }
                })
                .onPause(new OnPauseListener() {
                    @Override
                    public void pause() {
                        Log.d(TAG, "onPause");
                    }
                })
                .onLoadStart(new OnLoadStartListener() {
                    @Override
                    public void loadStart() {
                        Log.d(TAG, "onLoadStart");
                    }
                })
                .onStop(new OnStopListener() {
                    @Override
                    public void stop() {
                        Log.d(TAG, "onStop");
                    }
                })
                .onError(new OnErrorListener() {
                    @Override
                    public void error() {
                        Log.d(TAG, "onError");
                    }
                })
                .onEnterFullScreen(new OnEnterFullScreenListener() {
                    @Override
                    public void enterFullScreen() {
                        Log.d(TAG, "onEnterFullScreen");
                    }
                })
                .onExitFullScreen(new OnExitFullScreenListener() {
                    @Override
                    public void exitFullScreen() {
                        Log.d(TAG, "onExitFullScreen");
                    }
                })
                .onMute(new OnMuteListener() {
                    @Override
                    public void mute() {
                        Log.d(TAG, "onMute");
                    }
                })
                .onResumeVolume(new OnResumeVolumeListener() {
                    @Override
                    public void resumeVolume() {
                        Log.d(TAG, "onResumeVolume");
                    }
                })
                .onSetVolume(new OnSetVolumeListener() {
                    @Override
                    public void setVolume() {
                        Log.d(TAG, "onSetVolume");
                    }
                })
                .onSeekTo(new OnSeekToListener() {
                    @Override
                    public void seekTo(long pos) {
                        Log.d(TAG, "onSeekTo : " + pos);
                    }
                })
                .onMarqueeView(() -> {
                    Log.i(TAG, "on marquee click");
                })
                .load();


        DanmakuModel danmakuModel = new DanmakuModel();
        List<DanmakuDetail> danmakuDetails = new ArrayList<>();
        danmakuModel.setDanmakuDetails(danmakuDetails);
        danmakuModel.setScrollSpeed(1.2f);
        DanmakuDetail danmakuDetail = new DanmakuDetail();
        danmakuDetail.setType(1);
        danmakuDetail.setRelativeTime(3.0f);
        danmakuDetail.setText("啊啊啊啊啊啊啊啊啊啊啊啊");
        danmakuDetail.setColor(16777215);
        danmakuDetails.add(danmakuDetail);
        DanmakuDetail danmakuDetail2 = new DanmakuDetail();
        danmakuDetail2.setType(5);
        danmakuDetail2.setRelativeTime(6.0f);
        danmakuDetail2.setText("dsadasdsadadssd");
        danmakuDetail2.setColor(16776960);
        danmakuDetails.add(danmakuDetail2);
        DanmakuDetail danmakuDetail3 = new DanmakuDetail();
        danmakuDetail3.setType(4);
        danmakuDetail3.setRelativeTime(3.5f);
        danmakuDetail3.setText("弹幕来啦~~~~");
        danmakuDetail3.setColor(16711680);
        danmakuDetails.add(danmakuDetail3);

        // 启用弹幕，不打开弹幕
        mVideoView.enableDanmaku(danmakuModel, PlayerConstants.DanmakuInitType.CLOSE);
        // 启用投屏
        mVideoView.enableDLNA();
        // 自动播放
        mVideoView.start();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.release();
        // 避免内存泄漏
        MemoryLeakUtil.fixLeak(this);
    }


    @Override
    public void onBackPressed() {
        if (!mVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }

    //-------------------------以下为测试代码----------

    public void testStart(View view) {
        if (mVideoView != null) {
            mVideoView.start();
        }
    }

    public void testPause(View view) {
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    public void testResume(View view) {
        if (mVideoView != null) {
            mVideoView.resume();
        }
    }

    public void testStop(View view) {
        if (mVideoView != null) {
            mVideoView.stop();
        }
    }

    public void testSeekTo(View view) {
        if (mVideoView != null) {
            mVideoView.seekTo(9000);
        }
    }

    public void testGetDuration(View view) {
        if (mVideoView != null) {
            Toast.makeText(this, "当前视频时长：" + mVideoView.getDuration() + "ms", Toast.LENGTH_SHORT).show();
        }
    }

    public void testSetSpeed(View view) {
        if (mVideoView != null) {
            mVideoView.setSpeed(2.0f);
        }
    }

    public void testGetCurrentPosition(View view) {
        if (mVideoView != null) {
            Toast.makeText(this, "当前视频播放位置：" + mVideoView.getCurrentPosition() + "ms", Toast.LENGTH_SHORT).show();
        }
    }

    public void testIsMute(View view) {
        if (mVideoView != null) {
            Toast.makeText(this, "是否静音：" + mVideoView.isMute(), Toast.LENGTH_SHORT).show();
        }
    }

    public void testSetMute(View view) {
        if (mVideoView != null) {
            mVideoView.setMute(true);
        }
    }

    public void testResumeMute(View view) {
        if (mVideoView != null) {
            mVideoView.setMute(false);
        }
    }

    public void testEnterFullScreen(View view) {
        if (mVideoView != null) {
            mVideoView.doStartStopFullScreen();
        }
    }

    public void testGetBufferedPercentage(View view) {
        if (mVideoView != null) {
            Toast.makeText(this, "视频预加载百分百：" + mVideoView.getBufferedPercentage() + "%", Toast.LENGTH_SHORT).show();
        }
    }

    public void testStopStartAd(View view) {
        if (mVideoView != null) {
            mVideoView.stopStartAd();
        }
    }

    public void testStopEndAd(View view) {
        if (mVideoView != null) {
            mVideoView.stopEndAd();
        }
    }


    public void testFullScreenPortrait(View view) {
        if (mVideoView != null) {
            boolean enable = mVideoView.isFullScreenPortrait();
            mVideoView.setFullScreenPortrait(!enable);
        }
    }
}
